package com.tsj.model.auto.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsj.model.auto.model.entity.Website;
import com.tsj.model.auto.model.input.WebsiteAddInput;
import com.tsj.model.auto.service.IWebsiteService;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 网址表 前端控制器
 * </p>
 *
 * @author taoshujian
 * @since 2025-09-27 21:01:32
 */
@RestController
@RequestMapping("/website")
@Hidden
public class WebsiteController {

    @Resource
    private IWebsiteService websiteService;

    /**
     * 新增网站信息
     */
    @PostMapping
    public ResponseEntity<Website> addWebsite(@RequestBody Website website) {
        boolean saved = websiteService.save(website);
        if (saved) {
            // 假设保存成功后，实体类的ID会被自动填充
            return ResponseEntity.status(HttpStatus.CREATED).body(website);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 根据ID删除网站信息
     */
    @PostMapping("/{id}")
    public ResponseEntity<Void> deleteWebsite(@PathVariable Long id) {
        boolean deleted = websiteService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 更新网站信息
     */
    @PostMapping("/updateWebsite")
    public ResponseEntity<Website> updateWebsite(@RequestBody Website website) {
        boolean updated = websiteService.updateById(website);
        if (updated) {
            return ResponseEntity.ok(website);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据ID查询网站信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Website> getWebsiteById(@PathVariable Long id) {
        Website website = websiteService.getById(id);
        if (website != null) {
            return ResponseEntity.ok(website);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 查询所有网站信息
     */
    @GetMapping
    public ResponseEntity<List<Website>> getAllWebsites() {
        List<Website> websites = websiteService.list();
        return ResponseEntity.ok(websites);
    }

    /**
     * 分页查询网站信息
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Website>> getWebsitesByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<Website> page = new Page<>(pageNum, pageSize);
        IPage<Website> resultPage = websiteService.page(page);
        return ResponseEntity.ok(resultPage);
    }
}
